package com.zxlfysj.sort;

import java.util.Arrays;

/**
 * 选择排序
 * @author yangshujing
 * @create 2020-08-18 10:08
 */
public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {1,3,-2,10,9,11};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));

    }

    public static void selectSort(int[] arr) {
        for(int i = 0; i < arr.length - 1; i++) {
            int min = arr[i];
            int minIndex = i;
            for(int j = i + 1; j < arr.length; j++) {

                if(min > arr[j]) {
                    min = arr[j];
                    minIndex = j;

                }
            }
            if(minIndex != i) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }
}
